package mi.payment.settings;

import mi.payment.utils.SharedPreferencesUtil;

/**
 * 交易开关设置
 *
 * @author sunmi on 2017/2/12.
 */
public class TransactionSwitchOperation {

    private static TransactionSwitchOperation instance;
    private TransactionSwitchSetting instanceSettings;

    public static TransactionSwitchOperation getInstance() {
        if (instance == null) {
            instance = new TransactionSwitchOperation();
        }
        return instance;
    }

    private TransactionSwitchOperation() {
        instanceSettings = getTransactionSwitchSetting();
        if (instanceSettings == null) {
            instanceSettings = new TransactionSwitchSetting();
        }
        saveTransactionSwitchSetting(instanceSettings);
    }

    public TransactionSwitchSetting getTransactionSwitchSetting() {
        if (instanceSettings == null) {
            Object obj = SharedPreferencesUtil.readObj(TransactionSwitchSetting.TRANSACTION_SWITCH_KEY);
            if (obj instanceof TransactionSwitchSetting) {
                instanceSettings = (TransactionSwitchSetting) obj;
            } else {
                instanceSettings = new TransactionSwitchSetting();
                saveTransactionSwitchSetting(instanceSettings);
            }
        }
        return instanceSettings;
    }

    public void init(boolean consumption,
                     boolean consumptionRevoke,
                     boolean returnGoods,
                     boolean queryBalance,
                     boolean fieldAuth,
                     boolean fieldAuthRevoke,
                     boolean fieldAuthReq,
                     boolean fieldAuthCompleteRevocationNotice,
                     boolean fieldAuthCompleteRevocation) {
        TransactionSwitchSetting bean = getTransactionSwitchSetting();
        bean.setConsumption(consumption);
        bean.setConsumptionRevoke(consumptionRevoke);
        bean.setReturnGoods(returnGoods);
        bean.setQueryBalance(queryBalance);
        bean.setFieldAuth(fieldAuth);
        bean.setFieldAuthRevoke(fieldAuthRevoke);
        bean.setFieldAuthReq(fieldAuthReq);
        bean.setFieldAuthCompleteRevocationNotice(fieldAuthCompleteRevocationNotice);
        bean.setFieldAuthCompleteRevocation(fieldAuthCompleteRevocation);
        saveTransactionSwitchSetting(bean);
    }

    /**
     * 预授权完成撤销
     */
    public void saveFieldAuthCompleteRevocation(boolean fieldAuthCompleteRevocation) {
        TransactionSwitchSetting setting = getTransactionSwitchSetting();
        setting.setFieldAuthCompleteRevocation(fieldAuthCompleteRevocation);
        saveTransactionSwitchSetting(setting);
    }

    /**
     * 预授权完成（通知）
     */
    public void saveFieldAuthCompleteRevocationNotice(boolean fieldAuthCompleteRevocationNotice) {
        TransactionSwitchSetting setting = getTransactionSwitchSetting();
        setting.setFieldAuthCompleteRevocationNotice(fieldAuthCompleteRevocationNotice);
        saveTransactionSwitchSetting(setting);
    }

    /**
     * 预授权完成请求
     */
    public void saveFieldAuthReq(boolean fieldAuthReq) {
        TransactionSwitchSetting setting = getTransactionSwitchSetting();
        setting.setFieldAuthReq(fieldAuthReq);
        saveTransactionSwitchSetting(setting);
    }

    /**
     * 预授权撤销
     */
    public void saveFieldAuthRevoke(boolean fieldAuthRevoke) {
        TransactionSwitchSetting setting = getTransactionSwitchSetting();
        setting.setFieldAuthRevoke(fieldAuthRevoke);
        saveTransactionSwitchSetting(setting);
    }

    /**
     * 预授权
     */
    public void saveFieldAuth(boolean fieldAuth) {
        TransactionSwitchSetting setting = getTransactionSwitchSetting();
        setting.setFieldAuth(fieldAuth);
        saveTransactionSwitchSetting(setting);
    }

    /**
     * 查询余额
     */
    public void saveQueryBalance(boolean queryBalance) {
        TransactionSwitchSetting setting = getTransactionSwitchSetting();
        setting.setQueryBalance(queryBalance);
        saveTransactionSwitchSetting(setting);
    }

    /**
     * 退货
     */
    public void saveReturnGoods(boolean returnGoods) {
        TransactionSwitchSetting setting = getTransactionSwitchSetting();
        setting.setReturnGoods(returnGoods);
        saveTransactionSwitchSetting(setting);
    }

    /**
     * 消费撤销
     */
    public void saveConsumptionRevoke(boolean consumptionRevoke) {
        TransactionSwitchSetting setting = getTransactionSwitchSetting();
        setting.setConsumptionRevoke(consumptionRevoke);
        saveTransactionSwitchSetting(setting);
    }

    /**
     * 消费
     */
    public void saveConsumption(boolean consumption) {
        TransactionSwitchSetting setting = getTransactionSwitchSetting();
        setting.setConsumption(consumption);
        saveTransactionSwitchSetting(setting);
    }

    /**
     * 01账户自动签到
     */
    public void saveAutoCheckIn(boolean isAuto) {
        TransactionSwitchSetting setting = getTransactionSwitchSetting();
        setting.setAutoChecked(isAuto);
        saveTransactionSwitchSetting(setting);
    }


    /**
     * 微信主扫，商户扫客户
     */
    public void saveWeChatScan(boolean isUse) {
        TransactionSwitchSetting setting = getTransactionSwitchSetting();
        setting.setUseWeChatScan(isUse);
        saveTransactionSwitchSetting(setting);
    }

    /**
     * 微信被扫，客户扫商户
     */
    public void saveWeChatQR(boolean isUse) {
        TransactionSwitchSetting setting = getTransactionSwitchSetting();
        setting.setUseWeChatQR(isUse);
        saveTransactionSwitchSetting(setting);
    }


    /**
     * 支付宝主扫,商户扫客户
     */
    public void saveAliPayScan(boolean isUse) {
        TransactionSwitchSetting setting = getTransactionSwitchSetting();
        setting.setUseAliPayScan(isUse);
        saveTransactionSwitchSetting(setting);
    }

    /**
     * 支付宝被扫，客户扫商户
     */
    public void saveAliPayQR(boolean isUse) {
        TransactionSwitchSetting setting = getTransactionSwitchSetting();
        setting.setUseAliPayQR(isUse);
        saveTransactionSwitchSetting(setting);
    }

    /**
     * 口碑支付
     */
    public void saveKouBei(boolean isUse) {
        TransactionSwitchSetting setting = getTransactionSwitchSetting();
        setting.setUseKouBei(isUse);
        saveTransactionSwitchSetting(setting);
    }

    /**
     * 银联扫码主扫,商户扫客户
     */
    public void saveUnionPayScan(boolean isUse) {
        TransactionSwitchSetting setting = getTransactionSwitchSetting();
        setting.setUseUnionPayScan(isUse);
        saveTransactionSwitchSetting(setting);
    }

    /**
     * 银联扫码被扫，客户扫商户
     */
    public void saveUnionPayQR(boolean isUse) {
        TransactionSwitchSetting setting = getTransactionSwitchSetting();
        setting.setUseUnionPayQR(isUse);
        saveTransactionSwitchSetting(setting);
    }

    public void saveTransactionSwitchSetting(TransactionSwitchSetting transactionSwitchSetting) {
        this.instanceSettings = transactionSwitchSetting;
        SharedPreferencesUtil.saveObj(transactionSwitchSetting, TransactionSwitchSetting.TRANSACTION_SWITCH_KEY);
    }

    public boolean isConsumption() {
        TransactionSwitchSetting setting = getTransactionSwitchSetting();
        return setting.isConsumption();
    }

    public boolean isConsumptionRevoke() {
        TransactionSwitchSetting setting = getTransactionSwitchSetting();
        return setting.isConsumptionRevoke();

    }

    public boolean isReturnGoods() {
        TransactionSwitchSetting setting = getTransactionSwitchSetting();
        return setting.isReturnGoods();
    }

    public boolean isQueryBalance() {
        TransactionSwitchSetting setting = getTransactionSwitchSetting();
        return setting.isQueryBalance();
    }

    public boolean isFieldAuth() {
        TransactionSwitchSetting setting = getTransactionSwitchSetting();
        return setting.isFieldAuth();
    }

    public boolean isFieldAuthRevoke() {
        TransactionSwitchSetting setting = getTransactionSwitchSetting();
        return setting.isFieldAuthRevoke();
    }

    public boolean isFieldAuthReq() {
        TransactionSwitchSetting setting = getTransactionSwitchSetting();
        return setting.isFieldAuthReq();
    }

    public boolean isFieldAuthCompleteRevocationNotice() {
        TransactionSwitchSetting setting = getTransactionSwitchSetting();
        return setting.isFieldAuthCompleteRevocationNotice();
    }

    public boolean isFieldAuthCompleteRevocation() {
        TransactionSwitchSetting setting = getTransactionSwitchSetting();
        return setting.isFieldAuthCompleteRevocation();
    }


}
